package org.linlinjava.litemall.admin.controller;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallFootprint;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallFootprintService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 管理足迹控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/footprint")
@RequiredArgsConstructor
@Validated
public class AdminFootprintController {

    /**
     * 足迹服务
     */
    private final LitemallFootprintService footprintService;

    private final LitemallUserService litemallUserService;

    private final LitemallGoodsService litemallGoodsService;


    @GetMapping("setUser/{userId}")
    public ResponseUtil.Response setUser(@PathVariable("userId") Integer userId){
        LitemallUser user = new LitemallUser();
        user.setAvailableAmount(new BigDecimal(1000.00));
        user.setId(userId);
        boolean b = litemallUserService.updateById(user);
        return ResponseUtil.ok(b);
    }


    /**
     * 列表
     *
     * @param userId  用户id
     * @param goodsId 商品id
     * @param page    页面
     * @param limit   限制
     * @param sort    排序
     * @param order   订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String username, String goodsname,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        if(StrUtil.isNotBlank(username)){
            LitemallUser litemallUser = litemallUserService.getOne(Wrappers.lambdaQuery(LitemallUser.class)
                    .eq(LitemallUser::getUsername, username)
                    .eq(LitemallUser::getInnerAccount,0));
            username=ObjectUtil.isNotNull(litemallUser)?String.valueOf(litemallUser.getId()):username;
        }
        if(StrUtil.isNotBlank(goodsname)){
            LitemallGoods litemallGoods = litemallGoodsService.findByName(goodsname);
            goodsname=ObjectUtil.isNotNull(litemallGoods)?String.valueOf(litemallGoods.getId()):goodsname;
        }
        IPage<LitemallFootprint> footprintList = footprintService.querySelective(username, goodsname, page, limit, sort, order);
        footprintList.getRecords().stream().forEach(footprint->{
            footprint.setUsername(litemallUserService.findById(footprint.getUserId()).getUsername());
            footprint.setGoodsname(litemallGoodsService.findById(footprint.getGoodsId()).getName());

        });
        return ResponseUtil.okPage(footprintList);
    }
}
